﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
namespace Air_condition.DO
{
    [Serializable]
    public class Wall : Construction
    {
        // Attributes of wall object
        private int itypeExposure = -1;
        private double dbheatResistance;

        // Public access to attributes
        public int iTypeExposure
        {
            get { return itypeExposure; }
            set { itypeExposure = value; }
        }

        public double dbHeatResistance
        {
            get { return dbheatResistance; }
            set { dbheatResistance = value; }
        }

        //  Tính hiệu nhiệt độ dư
        /// <summary>
        /// Method to calculate temperature different (denta T)
        /// </summary>
        /// <param name="dbinsideRoomTemp">temperature inside room</param>
        /// <param name="dboutsideRoomTemp">temperature outside room</param>
        public override void CalculateTemperatureDiff(double dbinsideRoomTemp, double dboutsideRoomTemp)
        {

            // Tiếp xúc với ko khí ngoài trời
            // If room exposure with air
            if (itypeExposure == 0)
            {
                dbtemperatureDiff = dboutsideRoomTemp - dbinsideRoomTemp;
                dbheatTransferCoefficient = 1.0 / ((1.0 / 20.0) + dbheatResistance + (1.0 / 10.0));
            }
            else
            {
                // Tiếp xúc với ko khí của 1 ko gian ko có điều hòa
                // If room exposure with air of place without air-codition
                if (itypeExposure == 1)
                {
                    dbtemperatureDiff = 0.7 * (dboutsideRoomTemp - dbinsideRoomTemp);
                    dbheatTransferCoefficient = 1.0 / ((1.0 / 10.0) + dbheatResistance + (1.0 / 10.0));
                }
                else
                {
                    // If room exposure with air of place with air-codition
                    dbtemperatureDiff = 0;
                }
            }
        }

        /// <summary>
        /// Calculate heat transfer through wall
        /// </summary>
        /// <param name="dbinsideRoomTemp">inside room temperature</param>
        /// <param name="dboutsideRoomTemp">outside room temperature</param>
        /// <returns>Calculate heat transfer through wall</returns>
        public override double CalculateConstructionHeatTransfer(double dbinsideRoomTemp, double dboutsideRoomTemp)
        {
            CalculateTemperatureDiff(dbinsideRoomTemp, dboutsideRoomTemp);
            dbarea = dbWidth * dblength;
            return dbarea * dbheatTransferCoefficient * dbtemperatureDiff;
        }

        /// <summary>
        /// method to copy wall object to a new object
        /// </summary>
        /// <returns>a new object is wall object type</returns>
        public object Clone()
        {
            object clone;
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {

                    BinaryFormatter formatter = new BinaryFormatter();

                    // Serialize this object

                    formatter.Serialize(stream, this);

                    stream.Position = 0;

                    // Deserialize to another object

                    clone = formatter.Deserialize(stream);

                }
                return clone;
            }
            catch (Exception e)
            {
                return e;
            }

        }
    }
}
